<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="操作人" prop="backupUserId">
        <el-select v-model="queryParams.backupUserId" placeholder="请选择操作人">
          <el-option v-for="item in userList" :key="item.userId" :label="item.nickName"
            :value="item.userId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备份时间" prop="backupTime">
        <el-date-picker clearable v-model="queryParams.backupTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择备份时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['storage:dataBackupRecord:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['storage:dataBackupRecord:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['storage:dataBackupRecord:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['storage:dataBackupRecord:export']">导出</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="manualBackup" v-hasPermi="['storage:dataBackupRecord:generateRecord']">手动备份</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataBackupRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="备份标识" align="center" prop="backupId" />
      <el-table-column label="备份路径" align="center" prop="backupUrl" />
      <el-table-column label="备份类型" align="center" prop="backupType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.backup_type" :value="scope.row.backupType" />
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="backupUserId" v-if="false" />
      <el-table-column label="操作人" align="center" prop="nickName" />
      <el-table-column label="备份时间" align="center" prop="backupTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.backupTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注说明" align="center" prop="remark" />
      <el-table-column label="删除标识" align="center" prop="delflag" v-if="false">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dbr_delete_id" :value="scope.row.backupType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['storage:dataBackupRecord:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['storage:dataBackupRecord:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改数据备份记录对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备份路径" prop="backupUrl">
          <el-input v-model="form.backupUrl" placeholder="请输入备份路径" />
        </el-form-item>
        <el-form-item label="操作人" prop="backupUserId">
          <el-select v-model="form.backupUserId" placeholder="请选择操作人">
            <el-option v-for="item in userList" :key="item.deptId" :label="item.nickName"
              :value="item.deptId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备份时间" prop="backupTime">
          <el-date-picker clearable v-model="form.backupTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择备份时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备份类型" prop="backupType">
          <el-select v-model="form.backupType" placeholder="请选择备份类型" clearable>
            <el-option v-for="dict in dict.type.dbr_backup_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注说明" />
        </el-form-item>
        <el-form-item label="删除标识" prop="delflag">
          <el-select v-model="form.delflag" placeholder="请选择删除标识" clearable>
            <el-option v-for="dict in dict.type.dbr_delete_id" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { listDataBackupRecord,  generateDataBackupRecords } from "@/api/meter/dataBackupRecord";
import { listUser } from "@/api/system/user"

export default {
  name: "DataBackupRecord",
  dicts: ["backup_type", "dbr_delete_id"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据备份记录表格数据
      dataBackupRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        backupUrl: null,
        backupType: null,
        backupUserId: null,
        backupTime: null,
        delflag: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //用户列表
      userList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据备份记录列表 */
    getList() {
      this.loading = true;
      listDataBackupRecord(this.queryParams).then(response => {
        this.dataBackupRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      //查询用户列表
      const dateRange = []
      const queryParam = {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined
      }
      const delFlag = {
        delFlag: 0
      }
      listUser(queryParam, dateRange, delFlag).then(response => {
        this.userList = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        backupId: null,
        backupUrl: null,
        backupType: null,
        backupUserId: null,
        backupTime: null,
        remark: null,
        delflag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.backupId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //手动备份按钮操作
    manualBackup() {
      generateDataBackupRecords().then(response => {
        if (response.code == 200) {
          this.$message({
            message: '手动备份成功!',
            type: 'success'
          });
          this.getList()
        }else{
          this.$message.error('手动备份失败!');
          this.getList()
        }
      })
    }
  }
};
</script>
